﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Linq.Expressions;

namespace GenericValidation
{
    /// <summary>
    /// Internally used to expose an Entity member for custom member validation; ex: p => p.Name.
    /// Member Validators can be Comparing, String Lenght, RegEx.
    /// Any MemberValidationRule&lt;<typeparamref name="TEntity"/>, <typeparamref name="TMember"/>&gt; must reference such ValidationMember. 
    /// </summary>
    /// <typeparam name="TEntity">Entity type</typeparam>
    /// <typeparam name="TMember">Member type (ex: int, int?, string, DateTime, DateTime?, etc)</typeparam>
    public class ValidationMember<TEntity, TMember>
        where TEntity:class
    {
        internal ValidationMember(IValidation validation, Expression<Func<TEntity, TMember>> member)
        {
            this.Validation = validation;
            this.MemberExpression = member;
            if (member.Body is MemberExpression)
            {
                this.MemberName = (member.Body as MemberExpression).Member.Name;
            }
        }

        public IValidation Validation { get; set; }

        /// <summary>
        /// The validated Member name.
        /// </summary>
        public string MemberName { get; internal set; }

        /// <summary>
        /// The validated Member access Lambda Expression.
        /// </summary>
        public Expression<Func<TEntity, TMember>> MemberExpression { get; set; }

        public IDictionary<string, object> Data { get; set; }

        /// <summary>
        /// Localizes the Member Name value, with a resource having the <paramref name="memberKey"/> name.
        /// The Resource Manager must be properly registered in the Validation container.
        /// </summary>
        /// <param name="memberKey"></param>
        public virtual void Localize(string memberKey)
        {
            this.MemberName = this.Validation.LocalizeMember(memberKey);
        }

    }
}
